package com.itic.mall.mobile.jd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.banner.persistence.model.Banner;
import com.itic.mall.system.banner.persistence.model.BannerExample;
import com.itic.mall.system.banner.service.BannerService;
import com.itic.mall.system.categoryjd.persistence.model.CategoryJd;
import com.itic.mall.system.categoryjd.persistence.model.CategoryJdExample;
import com.itic.mall.system.categoryjd.service.CategoryJdService;
import com.itic.mall.system.orderjd.persistence.model.OrderJd;
import com.itic.mall.system.orderjd.persistence.model.OrderJdExample;
import com.itic.mall.system.orderjd.service.OrderJdService;
import com.itic.mall.system.orderjd.vo.OrderJdVo;
import com.itic.mall.system.orderjdproducts.persistence.model.OrderJdProducts;
import com.itic.mall.system.orderjdproducts.persistence.model.OrderJdProductsExample;
import com.itic.mall.system.orderjdproducts.service.OrderJdProductsService;
import com.itic.mall.system.productjd.persistence.model.ProductJd;
import com.itic.mall.system.productjd.persistence.model.ProductJdExample;
import com.itic.mall.system.productjd.persistence.model.ProductJdExample.Criteria;
import com.itic.mall.system.productjd.service.ProductJdService;
import com.itic.mall.system.user.persistence.model.MallUser;

/**
 * 友惠家京东商城接口类
 * 
 * @author 徐泽威 2017年08月17日 14时11分
 */
@Controller
@RequestMapping(value = "/mobile/jd")
public class MallJdInterface extends BaseMobileInterface {
    
    @Autowired 
    private ProductJdService productJdService;
    
    @Autowired 
    private OrderJdService orderJdService;
    
    @Autowired
    private BannerService bannerService;
    
    @Autowired
    private CategoryJdService categoryJdService;
    
    @Autowired
    private OrderJdProductsService orderJdProductsService;
    
    
    
    /**
     * APP用户登入京东商城首页
     * 
     * @return
     */
    @RequestMapping(value = "/test")
    @ResponseBody
    public Map<String, Object> test(HttpServletRequest request, HttpServletResponse response ) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {
        	String convertNew = MallJdUtils.convertNew(null, "1129313", null);
        	
            resultMap.put(DATA, convertNew);
            result = SUCCESS;
            errorCode = SUCCESS_ERROR_CODE;
            errorMsg = SUCCESS_ERROR_MSG;     
        	
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "list", e.toString(), "爆款列表", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }
    
    /**
     * APP用户登入京东商城首页
     * 
     * @return
     */
    @RequestMapping(value = "/create")
    @ResponseBody
    public Map<String, Object> create(HttpServletRequest request, HttpServletResponse response,String id ) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {
        	JSONObject userToChannel = MallJdUtils.userToChannel(id);
            resultMap.put(DATA, userToChannel);
            result = SUCCESS;
            errorCode = SUCCESS_ERROR_CODE;
            errorMsg = SUCCESS_ERROR_MSG;     
        	
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "list", e.toString(), "爆款列表", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }
    

    /**
     * APP用户登入京东商城首页
     * 
     * @return
     */
    @RequestMapping(value = "/index")
    @ResponseBody
    public Map<String, Object> index(HttpServletRequest request, HttpServletResponse response, String keyword,String categoryId,String orderType,String minMaxPrice,String minMaxSales ) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {
        	
        	Map<String, Object> dataMap = new HashMap<String, Object>();
        	//获取京东商品信息
            ProductJdExample example = new ProductJdExample();
            //example.setOrderByClause("REORDER ASC");
            if( StringHelper.isNotBlank(orderType) ){
    			if( "priceDesc".equals(orderType) ){
    				example.setOrderByClause("CON_PRICE ASC");
    			}
    			if( "priceAsc".equals(orderType) ){
    				example.setOrderByClause("CON_PRICE DESC");
    			}
    			if( "commissionAsc".equals(orderType) ){
    				example.setOrderByClause("COMMISSION_PROFIT DESC");
    			}
    			if( "commissionDesc".equals(orderType) ){
    				example.setOrderByClause("COMMISSION_PROFIT ASC");
    			}
    		}else{
    			//example.setOrderByClause("ORI_PRICE DESC");
    			//默认使用商品的创建时间倒序
    			example.setOrderByClause("UPDATE_TIME DESC");
    			example.setOrderByClause("REORDER ASC");
    		}
            example.setOrderByClause("CON_URL_B DESC");
            
            Criteria criteria = example.createCriteria();
            
            criteria.andIsEffectiveEqualTo(DBConst.TRUE);
            if( StringHelper.isNotBlank(keyword) ){
            	 criteria.andGoodsNameLike("%" + keyword + "%");
            }
            if( StringHelper.isNotBlank(categoryId) && !"0".equals(categoryId) ){
            	CategoryJd categoryJd = categoryJdService.get(categoryId);
            	if( null != categoryJd ){
            		criteria.andCategoryNameLike("%"+ categoryJd.getName() +"%");
            	}
            }
            
            //过滤价格区间
        	if( StringHelper.isNotBlank(minMaxPrice) ){
        		String[] split = minMaxPrice.split("-");
        		if(split.length==2){
        			String minPrice = split[0];
            		String maxPrice = split[1];
            		if( StringHelper.isNotBlank(minPrice) ){
            			criteria.andOriPriceGreaterThanOrEqualTo(Double.parseDouble(minPrice));
            		}
            		if( StringHelper.isNotBlank(maxPrice) ){
            			criteria.andOriPriceLessThanOrEqualTo(Double.parseDouble(maxPrice));
            		}
        		}
        	}
            handlePageQueryDefault(request, example);
            List<ProductJd> productJds = productJdService.list(example);
            dataMap.put("list", productJds);
            
            //获取轮播图信息
            BannerExample bannerExample = new BannerExample();
            bannerExample.setOrderByClause("UPDATE_TIME DESC");
            bannerExample.setOrderByClause("REORDER ASC");
            bannerExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andTypeEqualTo("003");
            List<Banner> banners = bannerService.list(bannerExample);
            dataMap.put("banners", banners);
            
            ProductJdExample example2 = new ProductJdExample();
            example2.setOrderByClause("UPDATE_TIME DESC");
            example2.setOrderByClause("ORDER_BAOKUAN ASC");
            Criteria createCriteria = example2.createCriteria();
            createCriteria.andIsEffectiveEqualTo(DBConst.TRUE);
            createCriteria.andIsBaokuanEqualTo("001");
            List<ProductJd> baokuans = productJdService.list(example2);
            
            dataMap.put("baokuans", baokuans);
            //dataMap.put("isMore", isMore(count,request));
            //dataMap.put("totalRecords", count);
            resultMap.put(DATA, dataMap);
            result = SUCCESS;
            errorCode = SUCCESS_ERROR_CODE;
            errorMsg = SUCCESS_ERROR_MSG;
        	
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "index", e.toString(), "登入京东商城首页", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }
    
    
    /**
     * APP用户登入京东商城首页
     * 
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request, HttpServletResponse response, String keyword, String categoryId, String minMaxPrice ) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {
        	
        	//获取京东商品信息
            ProductJdExample example = new ProductJdExample();
            //example.setOrderByClause("REORDER ASC");
            example.setOrderByClause("UPDATE_TIME DESC");
            example.setOrderByClause("REORDER ASC");
			//example.setOrderByClause("CREATE_TIME DESC");
            example.setOrderByClause("CON_URL_B DESC");
            
            Criteria criteria = example.createCriteria();
            
            criteria.andIsEffectiveEqualTo(DBConst.TRUE);
            if( StringHelper.isNotBlank(keyword) ){
            	 criteria.andGoodsNameLike("%" + keyword + "%");
            }
            if( StringHelper.isNotBlank(categoryId) && !"0".equals(categoryId) ){
            	CategoryJd categoryJd = categoryJdService.get(categoryId);
            	if( null != categoryJd ){
            		criteria.andCategoryNameLike("%"+ categoryJd.getName() +"%");
            	}
            }
            
            //过滤价格区间
        	if( StringHelper.isNotBlank(minMaxPrice) ){
        		String[] split = minMaxPrice.split("-");
        		if(split.length==2){
        			String minPrice = split[0];
            		String maxPrice = split[1];
            		if( StringHelper.isNotBlank(minPrice) ){
            			criteria.andOriPriceGreaterThanOrEqualTo(Double.parseDouble(minPrice));
            			
            		}
            		if( StringHelper.isNotBlank(maxPrice) ){
            			criteria.andOriPriceLessThanOrEqualTo(Double.parseDouble(maxPrice));
            		}
        		}
        	}
            
            
            handlePageQueryDefault(request, example);
            
            List<ProductJd> productJds = productJdService.list(example);
            
            //dataMap.put("isMore", isMore(count,request));
            //dataMap.put("totalRecords", count);
            resultMap.put(DATA, productJds);
            result = SUCCESS;
            errorCode = SUCCESS_ERROR_CODE;
            errorMsg = SUCCESS_ERROR_MSG;
        	
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "list", e.toString(), "京东列表", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }
    
    
    
    /**
     * APP用户登入京东商城首页
     * 
     * @return
     */
    @RequestMapping(value = "/baokuans")
    @ResponseBody
    public Map<String, Object> baokuans(HttpServletRequest request, HttpServletResponse response ) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {
        	
        	//获取京东商品信息
            ProductJdExample example = new ProductJdExample();
            //example.setOrderByClause("REORDER ASC");
            example.setOrderByClause("UPDATE_TIME DESC");
            example.setOrderByClause("ORDER_BAOKUAN ASC");
            
            Criteria criteria = example.createCriteria();
            criteria.andIsEffectiveEqualTo(DBConst.TRUE);
            criteria.andIsBaokuanEqualTo("001");
            
            List<ProductJd> baokuans = productJdService.list(example);
            
            //dataMap.put("isMore", isMore(count,request));
            //dataMap.put("totalRecords", count);
            resultMap.put(DATA, baokuans);
            result = SUCCESS;
            errorCode = SUCCESS_ERROR_CODE;
            errorMsg = SUCCESS_ERROR_MSG;
        	
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "list", e.toString(), "爆款列表", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }
    
    
    
    
    
    /**
     * APP用户登入商城中心首页
     * @return
     */
    @RequestMapping(value="/categorys")
    @ResponseBody
    public Map<String,Object> categorys(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//获取轮播图信息
    		CategoryJdExample example = new CategoryJdExample();
    		example.setOrderByClause("UPDATE_TIME DESC");
    		example.setOrderByClause("REORDER ASC");
    		example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
    		
            List<CategoryJd> list = categoryJdService.list(example);
            
            resultMap.put(DATA, list);
			result = SUCCESS;
			errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;  
    		
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("MallIndexInterface", "index", e.toString(),"登入商城中心首页",e);
		
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * APP用户登入商城中心首页
     * @return
     */
    @RequestMapping(value="/banners")
    @ResponseBody
    public Map<String,Object> banners(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//获取轮播图信息
            BannerExample bannerExample = new BannerExample();
            bannerExample.setOrderByClause("UPDATE_TIME DESC");
            bannerExample.setOrderByClause("REORDER ASC");
            bannerExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andTypeEqualTo("003");
            List<Banner> list = bannerService.list(bannerExample);
            resultMap.put(DATA, list);
			result = SUCCESS;
			errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;  
    		
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("MallIndexInterface", "index", e.toString(),"登入商城中心首页",e);
		
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * APP用户登入商城中心首页
     * @return
     */
    @RequestMapping(value="/seckills")
    @ResponseBody
    public Map<String,Object> seckills(HttpServletRequest request,HttpServletResponse response,String type){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(type) ){
    			//根据条件查找相应 的商品信息
        		ProductJdExample example = new ProductJdExample();
        		Criteria criteria = example.createCriteria();
        		example.setOrderByClause("CREATE_TIME DESC");
        		example.setOrderByClause("SECKILL_ORDER ASC");
        		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        		criteria.andSeckillTypeEqualTo(type);
        		
        		List<ProductJd> list = productJdService.list(example);
        		
            	resultMap.put(DATA, list);
    			result = SUCCESS;
    			errorCode = SUCCESS_ERROR_CODE;
            	errorMsg = SUCCESS_ERROR_MSG;  	
    		}
    		
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("MallIndexInterface", "index", e.toString(),"登入商城中心首页",e);
		
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    
    /**
     * APP用户登入京东商城首页
     * 
     * @return
     */
    @RequestMapping(value = "/more")
    @ResponseBody
    public Map<String, Object> more(HttpServletRequest request, HttpServletResponse response, String keyword ) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {
        	
        	Map<String, Object> dataMap = new HashMap<String, Object>();
            ProductJdExample example = new ProductJdExample();
            example.setOrderByClause("UPDATE_TIME DESC");
            example.setOrderByClause("REORDER ASC");
            this.handlePageQueryDefault(request, example);
            Criteria criteria = example.createCriteria();
            
            criteria.andIsEffectiveEqualTo(DBConst.TRUE);
            if( StringHelper.isNotBlank(keyword) ){
            	 criteria.andGoodsNameLike("%" + keyword + "%");
            }
            int count = productJdService.count(example);
            List<ProductJd> productJds = productJdService.list(example);
            
            dataMap.put("list", productJds);
            //dataMap.put("isMore", isMore(count,request));
            dataMap.put("totalRecords", count);
            resultMap.put(DATA, dataMap);
            result = SUCCESS;
            errorCode = SUCCESS_ERROR_CODE;
            errorMsg = SUCCESS_ERROR_MSG;
        	
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "index", e.toString(), "登入京东商城首页", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }
    
    /**
     * 获取二合一的地址
     * 
     * @return
     */
	@RequestMapping(value = "/convertSku")
    @ResponseBody
    public Map<String,Object> convertSku( HttpServletRequest request, HttpServletResponse response, String token, String id ){
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {

            MallUser user = getMallUserByToken(token);
            if (null != user) {
            	ProductJd productJd2 = productJdService.getBySku(id);
            	
            	if( null != productJd2 ){
            		
            		String tgwidByUserid = getTgwidByUserid(user.getId());
            		if( StringHelper.isNotBlank(tgwidByUserid) ){
            			String conUrl = "";
                		if( NumberStringHelper.isGreater(productJd2.getConTicketPrice(), "0") && StringHelper.isNotBlank(productJd2.getConUrlB()) ){
                			 conUrl = getConUrl(getTgwidByUserid(user.getId()), productJd2.getSku(), productJd2.getConUrlB());
                		}else{
                			 conUrl = getCommonUrl(getTgwidByUserid(user.getId()), productJd2.getSku() );
                		}
                		resultMap.put(DATA, conUrl);
                		result = SUCCESS;
                		errorCode = SUCCESS_ERROR_CODE;
                        errorMsg = SUCCESS_ERROR_MSG;
            		}else{
            			errorCode = TGWID_ERROR_CODE;
                        errorMsg = TGWID_ERROR_MSG;
            		}
            		
            	}else{
            		deleteProductJd(id);
                    result = FAIL;
                    errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
                    errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG;
            	}
            } else {
                result = LOGIN;
                errorCode = LOGIN_ERROR_CODE;
                errorMsg = LOGIN_ERROR_MSG;
            }
        
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "convert", e.toString(), "转成url链接", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }

    
    
    /**
     * 获取二合一的地址
     * 
     * @return
     
	@RequestMapping(value = "/convert")
    @ResponseBody
    public Map<String,Object> convert( HttpServletRequest request, HttpServletResponse response, String token, String id ){
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {

            MallUser user = getMallUserByToken(token);
            if (null != user) {
            	ProductJd productJd2 = productJdService.get(id);
            	
            	if( null != productJd2 ){
            		
            		String convertResult = MallJdUtils.convert(getYunIdByUser(user)+","+user.getUnionId(), productJd2.getSku(), productJd2.getConUrlB() );
            		jdCountStatics(getYunIdByUser(user)+","+user.getUnionId(), convertResult, productJd2.getSku());
            		if( null != convertResult ){
                   	    resultMap.put(DATA, convertResult );
                        result = SUCCESS;
                        errorCode = SUCCESS_ERROR_CODE;
                        errorMsg = SUCCESS_ERROR_MSG;
                   }else{
                	    //没有优惠卷就没有咯
	               	    String tgwIdByTgwName = getTgwIdByTgwName(getYunIdByUser(user)+","+user.getUnionId());
	               	    if( !StringHelper.isNotBlank(tgwIdByTgwName) ){
	               	    	tgwIdByTgwName = getTgwIdByTgwName("0");
	               	    }
	               	    String subConvert = MallJdUtils.subConvert(productJd2.getSku(), tgwIdByTgwName);
	           	    	if( null != subConvert ){
                      	    resultMap.put(DATA, subConvert );
                            result = SUCCESS;
                            errorCode = SUCCESS_ERROR_CODE;
                            errorMsg = SUCCESS_ERROR_MSG;
                       }else{
                      	    result = FAIL;
                            errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
                            errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG;
                       }
                   }
            		
            	}else{
            		deleteProductJd(id);
                    result = FAIL;
                    errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
                    errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG;
            	}
            } else {
                result = LOGIN;
                errorCode = LOGIN_ERROR_CODE;
                errorMsg = LOGIN_ERROR_MSG;
            }
        
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "convert", e.toString(), "转成url链接", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }
*/
	
	@RequestMapping(value = "/convert")
    @ResponseBody
    public Map<String,Object> convert( HttpServletRequest request, HttpServletResponse response, String token, String id ){
    	Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {

            MallUser user = getMallUserByToken(token);
            if (null != user) {
            	ProductJd productJd2 = productJdService.get(id);
            	
            	if( null != productJd2 ){
            		String tgwidByUserid = getTgwidByUserid(user.getId());
            		if( StringHelper.isNotBlank(tgwidByUserid) ){
            			String conUrl = "";
                		if( NumberStringHelper.isGreater(productJd2.getConTicketPrice(), "0") && StringHelper.isNotBlank(productJd2.getConUrlB()) ){
                			 conUrl = getConUrl(tgwidByUserid, productJd2.getSku(), productJd2.getConUrlB());
                		}else{
                			 conUrl = getCommonUrl(tgwidByUserid, productJd2.getSku() );
                		}
                		resultMap.put(DATA, conUrl);
                		result = SUCCESS;
                		errorCode = SUCCESS_ERROR_CODE;
                        errorMsg = SUCCESS_ERROR_MSG;
            		}else{
            			errorCode = TGWID_ERROR_CODE;
                        errorMsg = TGWID_ERROR_MSG;
            		}
            	}else{
            		deleteProductJd(id);
                    result = FAIL;
                    errorCode = JD_PRODUCT_EXPIRED_ERROR_CODE;
                    errorMsg = JD_PRODUCT_EXPIRED_ERROR_MSG;
            	}
            } else {
                result = LOGIN;
                errorCode = LOGIN_ERROR_CODE;
                errorMsg = LOGIN_ERROR_MSG;
            }
        
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "convert", e.toString(), "转成url链接", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    }
    
    /**
     * 查找所有京东商品信息
     * 
     * @param request
     * @param response
     * @param token
     * @param pageIndex
     * @param pageSize
     * @return
    */
    @RequestMapping(value = "/orders")
    @ResponseBody
    public Map<String, Object> orders(HttpServletRequest request, HttpServletResponse response, String token) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = FAIL;
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try {
            // 判断必要的参数
            if (StringHelper.isNotBlank(token)) {
                MallUser user = getMallUserByToken(token);
                if (null != user) {
                    Map<String, Object> dataMap = new HashMap<String, Object>();
                    OrderJdExample example = new OrderJdExample();
                    example.setOrderByClause("CREATE_TIME DESC");
                    com.itic.mall.system.orderjd.persistence.model.OrderJdExample.Criteria criteria = example.createCriteria();
                    criteria.andIsEffectiveEqualTo(DBConst.TRUE);
                    criteria.andUserIdEqualTo(user.getId());
                    this.handlePageQueryDefault(request, example);
                    int count = orderJdService.count(example);
                    List<OrderJd> productJds = orderJdService.list(example);
                    ArrayList<OrderJdVo> list = new ArrayList<OrderJdVo>();
                    if( productJds.size()>0 ){
                    	for( OrderJd tmp : productJds ){
                    		OrderJdVo orderJdVo = new OrderJdVo();
                    		orderJdVo.setOrderToatlCount(tmp.getOrderToatlCount());
                    		orderJdVo.setOrderTotalMoney(tmp.getOrderTotalMoney());
                    		orderJdVo.setOrderStatus(tmp.getOrderStatus());
                    		orderJdVo.setOrderDate(tmp.getOrderDate());
                    		
                    		OrderJdProductsExample example2 = new OrderJdProductsExample();
                    		example2.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andOrderIdEqualTo(tmp.getId());
                    		List<OrderJdProducts> products = orderJdProductsService.list(example2);
                    		orderJdVo.setProducts(products);
                    		
                    		list.add(orderJdVo);
                    	}
                    }
                    
                    dataMap.put("list", list);
                    dataMap.put("totalRecords", count);
                    resultMap.put(DATA, dataMap);
                    result = SUCCESS;
                    errorCode = SUCCESS_ERROR_CODE;
                    errorMsg = SUCCESS_ERROR_MSG;
                } else {
                    result = LOGIN;
                    errorCode = LOGIN_ERROR_CODE;
                    errorMsg = LOGIN_ERROR_MSG;
                }
            }
        } catch (Exception e) {
            errorCode = SERVER_ERROR_CODE;
            errorMsg = SERVER_ERROR_MSG;
            // log(e);
            logException("MallJdInterface", "queryProductList", e.toString(), "查找所有京东商品信息", e);

        }
        resultMap.put(RESULT, result);
        resultMap.put(ERROR_CODE, errorCode);
        resultMap.put(ERROR_MSG, errorMsg);
        isCrrossDomain(response);
        return resultMap;
    } 
     
}